import sys
from time import time
sys.path.insert(0,'../..')
from rmpoly import *
from gmpy import mpq

try:
  ntest = sys.argv[1]
  h = int(sys.argv[2])
except:
  print 'prog ntest h'
  sys.exit()

bwrite = 0
# see http://sage.math.washington.edu/home/fredrik/flint/timings.html
def bench1(n):
  rp,x = rgens('x',12,mpq)
  t0 = time()
  p1 = (1 + x + x**2).nth_root(-2,'x',n) + x
  p2 = p1.sqrt('x',n)
  t1 = time()
  if bwrite:
    print p2
  print "sqrt(x+1/sqrt(1+x+x^2)) prec=%d t=%.4f" %(n,t1-t0)

def bench1a(n):
  rp,a,x = rgens('a,x',10,mpq)
  t0 = time()
  p1 = (1 + a*x + 2*a*x**2).nth_root(-2,'x',n) + x
  p2 = p1.nth_root(2,'x',n)
  t1 = time()
  if bwrite:
    print p2
  print '%.4f' %(t1-t0)


def bench2(n):
  rp,x = rgens('x',10,mpq)
  t0 = time()
  p1 = (1 + x + x**2).log('x',n) + 1
  t1 = time()
  p2 = p1.log('x',n)
  t2 = time()
  if bwrite:
    print p2
  #print 'DB1 %.2f' %(t1-t0)
  print 'log(1+log(1+x+x^2)) prec=%d t=%.4f' %(n,t2-t0)

def bench2a(n):
  rp,a,x = rgens('a,x',10,mpq)
  t0 = time()
  p1 = (1 + a*x + 2*a*x**2).log('x',n) + 1
  p2 = p1.log('x',n)
  t1 = time()
  if bwrite:
    print p2
  print '%.4f' %(t1-t0)

def bench3(n):
  rp,x = rgens('x',12,mpq)
  t0 = time()
  p1 = 1 - (x + x**2).exp('x',n)
  t1 = time()
  p2 = p1._exp1('x',n)
  t2 = time()
  if bwrite:
    print p2
  #print 'DB1 %.2f' %(t1-t0)
  print 'exp(1-exp(x+x^2)) prec=%d t=%.4f' %(n,t2-t0)

def bench3a(n):
  rp,a,x = rgens('a,x',12,mpq)
  t0 = time()
  p1 = 1 - (x + a*x**2 + 2*a*x**2).exp('x',n)
  t1 = time()
  p2 = p1.exp1('x',n)
  t2 = time()
  if bwrite:
    print p2
  #print 'DB1 %.2f' %(t1-t0)
  print '%.4f' %(t2-t0)


f = eval('bench' + ntest)
f(h)

